package com.ebelter.btcomlib.bases.fragment;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import com.ebelter.btcomlib.models.bluetooth.products.bracelet.bean.Rate3Data;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.btcomlib.views.dialogs.LoadingDialog;

import java.lang.ref.WeakReference;




public abstract class BaseFragment extends Fragment {


  //  protected String TAG = "BaseFragment";
    protected Activity mActivity;
    protected View mRootView;

    public MyHandle myHandle;

    public static final String N= "0";
    public static final String N3= "0.0";
    public static final String N2= "--";

    private LoadingDialog loadingDialog;
    public void showLoadingDialog(String tip){
        if (loadingDialog==null){
            loadingDialog=new LoadingDialog(getContext());
        }
        if (!loadingDialog.isShowing()){
            loadingDialog.show();
        }
        loadingDialog.setTip(tip);

    }

    public void dissLoadingDialog(){
        if (loadingDialog!=null&&loadingDialog.isShowing()){
            loadingDialog.cancel();
        }
    }




    @SuppressLint("HandlerLeak")
    class MyHandle extends Handler {

        private WeakReference<Fragment> weakReference;

        MyHandle(Fragment fragment) {
            super(Looper.getMainLooper());
            this.weakReference = new WeakReference<>(fragment);
        }

        private void setWeakReference2Null(){
            if (weakReference!=null){
                weakReference.clear();
                weakReference = null;
            }
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (weakReference!=null&&weakReference.get()!=null){
                handleMsg(msg);
            }

        }
    }

    public void post(Runnable runnable){
        if (myHandle!=null){
            myHandle.post(runnable);
        }
    }

    protected void handleMsg(Message msg) {
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        //TAG = getClass().getSimpleName();
        mActivity = getActivity();
        myHandle=new MyHandle(this);
    }


    @Override
    @Nullable
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState)
    {
        mRootView = inflater.inflate(setLayoutResouceId(), container, false);
        initView();
        setListener();
        return mRootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData(getArguments());
    }

    protected abstract int setLayoutResouceId();

    /**
     * 初始化数据
     * @param arguments 接收到的从其他地方传递过来的参数
     */
    protected void initData(Bundle arguments)
    {

    }

    /**
     * 初始化View
     */
    protected void initView()
    {

    }

    /**
     * 设置监听事件
     */
    protected void setListener()
    {

    }




    @SuppressWarnings("unchecked")
    protected <T extends View> T findViewById(int id)
    {
        if (mRootView == null)
        {
            return null;
        }

        return (T) mRootView.findViewById(id);
    }
}
